import { ref } from 'vue'
import { adapter, request } from '@/request'
import { priceAskWordByGet } from '@/core/api/priceSheet'
import type { PriceSheet } from '@/core/interfaces/priceSheet'
import type { RequestWrapper } from '@/core/interfaces/common'
import type { PriceKeyword } from '@/core/interfaces/keyword'

interface Params {
  priceAskId: PriceSheet['id']
}

export default function getPriceAskWords() {
  const service = adapter<RequestWrapper<PriceKeyword[]>>(request)
  const loading = ref(false)
  const response = ref<PriceKeyword[]>([])

  const fetch = (params: Params) => {
    const { url, method } = priceAskWordByGet()
    loading.value = true

    return service({
      config: {
        url,
        method,
        params
      },
      callback: (res) => {
        response.value = res.data.data.map((item) => {
          return {
            ...item,
            frontKey: `${item.name}|-|${item.clientType}`,
            selected: false,
            reasons: item.showReasons ? item.showReasons.split(',') : [],
            loading: false,
            disabled: false
          }
        })
      },
      error: (err) => {
        console.log(err)
      },
      done: () => {
        loading.value = false
      }
    })
  }

  return [response, fetch, loading] as const
}
